# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=jack2
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.9.22
pkgrel=1
pkgdesc='The JACK low-latency audio server (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64')
url='https://jackaudio.org/'
license=('GPL2')
depends=("${MINGW_PACKAGE_PREFIX}-db"
         "${MINGW_PACKAGE_PREFIX}-libsystre"
         "${MINGW_PACKAGE_PREFIX}-opus"
         "${MINGW_PACKAGE_PREFIX}-portaudio"
         "${MINGW_PACKAGE_PREFIX}-readline"
         "${MINGW_PACKAGE_PREFIX}-libsamplerate"
         "${MINGW_PACKAGE_PREFIX}-libsndfile")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "python")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/jackaudio/jack2/archive/v${pkgver}.tar.gz"
        0001-wscript-remove-hardcoded-includedir.patch
        0003-relocate-plugins-libdir.patch
        0004-relocate-jackd-path.patch)
sha256sums=('1e42b9fc4ad7db7befd414d45ab2f8a159c0b30fcd6eee452be662298766a849'
            '5a343bfee863dd648e40161f59f197823092e215dd1daa04511ac4dbfb7bcf63'
            '797adc210372c18a49f3c02a91a27d615c843b1eacca29c738ccac2c8664dfe3'
            'a81826c43991b5add021b259abbf86f623808b7e1ed1c5cc6d8b6cb06b7ad1d5')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/0001-wscript-remove-hardcoded-includedir.patch"

  # TODO: If posible replace these with pathtools
  patch -p1 -i "${srcdir}/0003-relocate-plugins-libdir.patch"
  patch -p1 -i "${srcdir}/0004-relocate-jackd-path.patch"
}

build() {
  cd "${srcdir}/${_realname}-${pkgver}"

  export LINKFLAGS="${LDFLAGS}"

  export WAF_NO_PREFORK=1

  /usr/bin/python ./waf configure \
    --prefix=${MINGW_PREFIX} \
    --check-c-compiler=${CC} \
    --check-cxx-compiler=${CXX} \
    --platform=win32 \
    --out="${srcdir}/build-${MSYSTEM}"

  /usr/bin/python ./waf build
}

package() {
  cd "${srcdir}/${_realname}-${pkgver}"
  /usr/bin/python ./waf install --destdir="${pkgdir}"

  # Workaround: Move jack DLLs to bindir but not the plugins
  mv -v "${pkgdir}${MINGW_PREFIX}"/lib/*.dll "${pkgdir}${MINGW_PREFIX}/bin/"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
